<?php
if ( $_SESSION['logged_in'] == FALSE ) redirect (base_url ());
/*
 * Allows the controller to pass data gere and a grid will be displayed
 * 
 */

?>
<div id="contentfull">
<?php
if (isset($gridtype)) {
    switch ($gridtype) {
        case 'users':
            ListUsers($users); 
            break;
        case 'prop':
            //var_dump($property);
            ListProperty($property, $areas, $baseurl, $filt); 
            break;
        case 'menu':
            //var_dump($property);
            Listmenu($menu, $toplevel, $baseurl); 
            break;
        case 'reg':
           // var_dump($regdata);
            Listreg($regdata, $baseurl); 
            break;
        default:
            break;
    }
}
     
      echo '</div> <!-- content -->';

function Listreg($regd, $baseurl) {
    /*
     * Lists the registrations
     * 'id', 'Name', 'action', 'status', 'emailvrified', 'address'
     */   
    echo "<div class='form1'>";
    echo '<table width="90%" border="0" cellspacing="0" cellpadding="0" id="wide-content" overflow="auto">';
    echo '
        <thead id="gridheader">
            <th align="left" bgcolor="#9DE3F0" width="30%">Name</th>
            <th align="left" bgcolor="#9DE3F0" width="30%">Action</th>
            <th align="left" bgcolor="#9DE3F0" width="20%">Status</th>
            <th align="left" bgcolor="#9DE3F0" width="20%">Email Verified</th>
        </thead> ';
    
    // var_dump($regd);
    if (!$regd) { echo '<tr><td> NO DATA </td></tr>'; } else {
        
    foreach($regd as $value)
    {
      //  var_dump($value);
   	echo "<tr overflow='auto'>
              <td id='gridselect' >
              <a href='".$baseurl."index.php/manreg/";
   	echo $value['rid']."'>" ;
   	echo $value['Name']."</a>   </td> "; 
   	echo "<td  id='griddata'>" . $value['username'] ."</td>";
        echo "<td  id='griddata'>" . $value['mobile'] ."</td>";
        echo "<td  id='griddata'>" . $value['emailvrified'] ."</td>";
        echo "</tr>";
    }
    }
    echo "</table> ";
    echo "</div>";
}      
      
      
function ListUsers($userlist) {
    // displays Users entries in a table with the first column being a hyperlink to edit the entry    
    echo "<div class='form1'>";
    echo '<table width="90%" border="0" cellspacing="0" cellpadding="0" id="wide-content" overflow="auto">';
    echo '
        <thead id="gridheader">
        <th align="left" bgcolor="#9DE3F0" width="30%">Name</th>
        <th align="left" bgcolor="#9DE3F0" width="30%">Email</th>
        <th align="left" bgcolor="#9DE3F0" width="20%">Mobile</th>
        </thead> ';
   
    foreach($userlist as $value)
    {
   	echo "<tr overflow='auto'><td id='gridselect' ><a href='edituser/";
   	echo $value['id']."'>" ;
   	echo $value['Name']  . "</a></td> "; 
   	echo "<td  id='griddata'>" . $value['username'] ."</td>";
        echo "<td  id='griddata'>" . $value['mobile'] ."</td>";
        echo "</tr>";
    }
    echo "</table> ";
    echo "</div>";
}


function ListProperty($property, $areas, $baseurl, $filt) {
    
    // displays Users entries in a table with the first column being a hyperlink to edit the entry    
    // Property {'_id' = new mongoid(), 'Name' => 'Fosters', 'roadnum' => '104', 'street' => 'Watercombe Road', 
    // 'intersect' => 'Homestead & Watercombe', 'gps' => [{'longitude' : 0, 'latutude' => 0}], 'area' : [{'aid' => '', 'areaname' => ''}],
    // 'Owner' => [{'oid' => '', 'Ownername' => ''}], 'notes' => '' })
    // Create the Menubar
        echo '<div> ';
        echo form_open($baseurl.'index.php/listprop/', '');
        echo '<table><tr>';
      //  echo form_fieldset('<h2> Filter Menu </h2>');
        echo '<td>'; echo form_label('Set Filter', 'filter');  $filter = array('name' => 'filter', 'id' => 'filter' );
      
        // This chunk of code only shows the arears that are linked
       // var_dump($areas);
         $options = [];
         $options = array_merge($options, array('all' => 'No Filter Selected '));
         foreach($areas as $value)
        {
            $mid = $value['id'] ;
            $t = array($mid => $value['Area']);
            $options = array_merge($options, $t); 
        }
        echo '</td> 
            <td>';
        echo form_dropdown('filter', $options, $filt); 
        echo '</td>
            <td  align="right" > ';
        echo form_submit('filt', 'Set Filter');
        
        //   form_fieldset_close();
        form_close(); 
    echo '&nbsp;&nbsp;&nbsp;<a href="propertyadd" ><img class="btnimg" name="addimg3" id="1" src="'.$baseurl.'public/images/add.png" alt="Add a Property" title="Add a Property"/></a>';
    echo '</td></div>';
    
    echo "<div class='form1'>";
    echo '<table width="90%" border="0" cellspacing="0" cellpadding="0" id="wide-content" overflow="auto">';
    echo '
        <thead id="gridheader">
            <th align="left" bgcolor="#9DE3F0" width="20%">Name</th>
            <th align="left" bgcolor="#9DE3F0" width="20%">Address</th>
            <th align="left" bgcolor="#9DE3F0" width="20%">Intersection</th>
            <th align="left" bgcolor="#9DE3F0" width="20%">Area</th>
            <th align="left" bgcolor="#9DE3F0" width="20%">Owner</th>
        </thead> ';
    //var_dump($property);
    foreach($property as $value)
    {
       // var_dump($value['area'][0]['areaname']);
       // $a = $value['area']['0']['areaname'];
   	echo "<tr overflow='auto'><td id='gridselect' ><a href='propertyedit/";
        echo $value['id']."'>" ;
        echo $value['Name']  . "</a></td> "; 
        echo "<td id='griddata'>".$value['roadnum']." ".$value['street']."</td>";
        echo "<td id='griddata'>".$value['intersect']."</td>";
   //   echo "<td id='griddata'>".$a."</td>";
//        echo "<td id='griddata'>".$value['Owner']['Ownername']."</td>";
        echo "</tr>";
    }
    echo "</table> ";
    echo "</div>";
}

function Listmenu($menu, $toplevel, $baseurl) {
    // { "_id" : ObjectId("51236c16edd6b96b15b6653c"), "name" : "Home", "pages" : [ "all" ], 
    // "parent" : "none", "roles" : [ "all" ], "tags" : [ "none" ], "url" : "http://localhost/comunity/" }
    
    // Create the Menubar
        echo '<div> ';
        echo form_open($baseurl.'index.php/menulist/', '');
        echo '<table><tr>';
      //  echo form_fieldset('<h2> Filter Menu </h2>');
        echo '<td>'; echo form_label('Set Filter', 'filter');  $filter = array('name' => 'filter', 'id' => 'filter' );
      
        // This chunk of code only shows the arears that are linked
       // var_dump($areas);
         $options = [];
         $options = array_merge($options, array('all' => 'No Filter Selected '));
         $options = array_merge($options, array('none' => 'Top Level '));
         foreach($toplevel as $value)
        {
            $mid = $value['name'] ;
            $t = array($mid => $value['name']);
            $options = array_merge($options, $t); 
        }
        echo '</td> 
            <td>';
        echo form_dropdown('filter', $options); 
        echo '</td>
            <td  align="right" > ';
        echo form_submit('filt', 'Set Filter');
        
        //   form_fieldset_close();
        form_close(); 
    echo '&nbsp;&nbsp;&nbsp;<a href="modmenu/new" ><img class="btnimg" name="addimg3" id="1" src="'.$baseurl.'public/images/add.png" alt="Add Menu Item" title="Add Menu" /></a>';
    echo '</td></div>';
    
    echo "<div class='form1'>";
    echo '<table width="90%" border="0" cellspacing="0" cellpadding="0" id="wide-content" overflow="auto">';
    echo '
        <thead id="gridheader">
            <th align="left" bgcolor="#9DE3F0" width="20%">Name</th>
            <th align="left" bgcolor="#9DE3F0" width="20%">Parent</th>
            <th align="left" bgcolor="#9DE3F0" width="20%">url</th>
            <th align="left" bgcolor="#9DE3F0" width="20%">pages</th>
            <th align="left" bgcolor="#9DE3F0" width="20%">roles</th>
        </thead> ';
   
    foreach($menu as $value)
    {
       // var_dump($value);
       // $a = $value['area']['0']['areaname'];
   	echo "<tr overflow='auto'><td id='gridselect' ><a href='modmenu/";
        echo $value['_id']."'>" ;
        echo $value['name']  . "</a></td> "; 
        echo "<td id='griddata'>".$value['parent']."</td>";
        echo "<td id='griddata'>".$value['url']."</td>";
        $pages = '';
        if ($value['pages'] !== FALSE ) {
        foreach ($value['pages'] as $page) {
             $pages = $pages .' '. $page;
        } }
        echo "<td id='griddata'>".$pages."</td>";
        $roles = '';
        if ($value['pages'] !== FALSE ) {
        foreach ($value['pages'] as $role) {
             $roles = $roles .' '. $role;
        } }
        echo "<td id='griddata'>".$roles."</td>";
        echo "</tr>";
    }
    echo "</table> ";
    echo "</div>";

}
?>

